﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class EditProduct : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            if (Request["id"] != null && !"".Equals(Request["id"]) && StringUtil.IsPositiveNumber(Request["id"]))
            {
                ProductBA productBA = new ProductBA();
                int ID = Convert.ToInt32(Request["id"]);
                ProductTO product = productBA.GetProductByID(ID);
                Attributes.Add("product",product);
                ProductCategoryBA productCategoryBA = new ProductCategoryBA();
                List<ProductCategoryTO> listProductCategory = productCategoryBA.GetAllProductCategory();
                Attributes.Add("listProductCategory", listProductCategory);
            }
        }

        public void Update(HttpRequest Request,HttpResponse Response)
        {
            if (Request["id"] != null && !"".Equals(Request["id"]) && StringUtil.IsPositiveNumber(Request["id"]))
            {

                int ID = Convert.ToInt32(Request["id"]);
                ProductBA productBA = new ProductBA();
                ProductTO product = productBA.GetProductByID(ID);
                if (product == null)
                {
                    Attributes.Add("err", "Product is invalid");
                    this.isLoad = true;
                    return;
                }

                if (Request["txtCategory"] == null || "".Equals(Request["txtCategory"]) || !StringUtil.IsPositiveNumber(Request["txtCategory"]))
                {
                    Attributes.Add("err", "Product is invalid");
                    this.isLoad = true;
                    return;
                }

                if (product.Status == Constant.PRODUCT_ACTIVE_AUCTION)
                {
                    Attributes.Add("err", "Product is invalid");
                    this.isLoad = true;
                    return;
                }

                ProductCategoryBA productCategoryBA = new ProductCategoryBA();
                int productCategory = Convert.ToInt32(Request["txtCategory"]);
                ProductCategoryTO productCategoryTO = productCategoryBA.GetProductCategoryByID(productCategory);
                if (productCategoryTO == null)
                {
                    Attributes.Add("err", "Product is invalid");
                    this.isLoad = true;
                    return;
                }

                int status = Constant.PRODUCT_ACTIVE_AUCTION;
                String myString = Request["beginDay"];
                String myHour = Request["beginHour"];
                DateTime beginDate;
                if (!DateTime.TryParseExact(myString + " " + myHour, "dd/MM/yyyy hh:mm", null, DateTimeStyles.None, out beginDate))
                {                    
                    Attributes.Add("err", "Begin Date is invalid");
                    this.isLoad = true;
                    return;
                }
                Boolean stt = productBA.UpdateProductByParam(ID, status, beginDate, productCategory);
                if (stt)
                {
                    Attributes.Add("success", "Update successfully");
                    Response.Redirect("/ViewProduct.aspx");
                }
                else
                {
                    this.isLoad = true;
                    Attributes.Add("err", "Update faulty");
                }
            }
        }


      


    }
}